package com.flashcash.solu.common.network;

import android.text.TextUtils;

import com.flashcash.solu.app.App;
import com.flashcash.solu.broadcast.InstallReferrerReceiver;
import com.flashcash.solu.common.TokenManager;
import com.flashcash.solu.harvester.TraceSender;
import com.flashcash.solu.harvester.uploadout.UploadUtils;
import com.flashcash.solu.widget.XLeoSp;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by XLEO on 2018/1/30.
 */

public class FirebaseHeaderInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request newRequest = chain.request();

        try {
            if (newRequest != null) {

                Request.Builder builder = chain.request().newBuilder();

                builder.addHeader("X-REFERRER", getGAInstallReferrer())
                        .addHeader("X-REFERRER-SDK", getGAInstallReferrerFromGASDK())
                        .addHeader("X-ANDROID-ID", TraceSender.getAndroidID(App.instance));

                //登录后的上传
                if (!existHeader(newRequest.headers(), "X-AUTH-TOKEN") &&
                        TokenManager.getInstance().getToken() != null) {
                    builder.addHeader("X-AUTH-TOKEN", TokenManager.getInstance().getToken());
                }

                newRequest = builder.build();

                uploadHeader(newRequest);

            }
        } catch (Exception e) {
            e.printStackTrace();

            UploadUtils.uploadException(e, "FirebaseHeaderInterceptor.intercept");
        }

        return chain.proceed(newRequest);
    }

    private static boolean existHeader(Headers headers, String header){
        if(headers == null || headers.names() == null ||header == null || headers.size() == 0 || header.length() ==0 ){
            return false;
        }

        return headers.names().contains(header);
    }
    private static String install_referrer_uploaded_key = "install_referrer_uploaded_key";
    private static String install_referrer_uploaded_with_token_key =
            "install_referrer_uploaded_with_token_key";

    //just log
    private static void uploadHeader(Request newRequest) {
        try {
            if (!TextUtils.isEmpty(getGAInstallReferrer())) {
                boolean referUploaded = XLeoSp.getInstance(App.instance).getBoolean
                        (install_referrer_uploaded_key);
                if (!referUploaded) {
                }

                boolean referWithTokenUploaded = XLeoSp.getInstance(App.instance).getBoolean
                        (install_referrer_uploaded_with_token_key);
                if (!referWithTokenUploaded && TokenManager.getInstance().getToken() != null) {
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            try {
                UploadUtils.uploadException(e, "FirebaseHeaderInterceptor.uploadHeader");
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    private static String getGAInstallReferrer() {
        return XLeoSp.getInstance(App.instance)
                .getString(InstallReferrerReceiver.install_referrer_store_key, "");
    }

    private static String getGAInstallReferrerFromGASDK() {
        return XLeoSp.getInstance(App.instance)
                .getString(InstallReferrerReceiver.install_referrer_from_ga_sdk_store_key, "");
    }

}
